//
//  FPRepayPlanMainView.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/23.
//

#import "FPRepayPlanMainView.h"
#import "FPRepayPlanCell.h"
#import "FPRepayPlanModel.h"
#import "FPLoanDetailModel.h"

@interface FPRepayPlanMainView () <UITableViewDelegate , UITableViewDataSource>

@property (nonatomic , strong) UITableView *tableView;

@end

@implementation FPRepayPlanMainView

- (void)setTerm:(NSUInteger)term {
    
    _term = term;
}

- (void)setDataSource:(NSArray *)dataSource {
    
    _dataSource = dataSource;
    
    CGFloat height;
    if (dataSource.count < 6) {
        
        height = 60.f * dataSource.count;
    }
    else {
        
        height = 60.f * 6;
    }
    
    self.tableView.frame = CGRectMake(0.f, 0.f, SCREEN_WIDTH - 48.f, height);
    
    double rate = 0.f;
    
    for (FPRepayPlanModel *model in dataSource) {
        
        rate += model.interest.doubleValue;
    }
    
    NSString *count = [FPLoanDetailModel termOfParam:[NSString stringWithFormat:@"%zd",self.term]];
    
    self.tipLab1.text = [NSString stringWithFormat:@"借满%@个月，每月还款金额相同", count];
    self.tipLab2.text = [NSString stringWithFormat:@"期满%@个月时，总利息：¥%.2f元", count, rate];
    
    [self.tableView reloadData];
}



- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    self.titleLab.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab1.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab2.textColor = [UIColor hexColor:@"#6A6A6A"];
    
    self.topView.clipsToBounds = YES;
    self.topView.layer.cornerRadius = 8.f;
    self.topView.backgroundColor = [UIColor hexColor:@"#F3F5F9"];
    
    self.confirmBtn.clipsToBounds = YES;
    self.confirmBtn.layer.cornerRadius = 22.f;
    self.confirmBtn.backgroundColor = MAIN_BLUE_COLOR;
    
    UIColor *color1 = [UIColor hexColor:@"#E8F1FF"];
    UIColor *color2 = [UIColor hexColor:@"#FFFFFF"];
    CAGradientLayer *layer = [UIColor applyGradientWithColor1:color1 color2:color2 frame:CGRectMake(0.f, 0.f, SCREEN_WIDTH, 90.f) direction:(GradientApiDirectionTopToBotton)];
    [self.layer insertSublayer:layer atIndex:0];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.backView.bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.backView addSubview:self.tableView];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.rowHeight = 60.f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPRepayPlanCell *cell;
    
    if (cell == nil) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPRepayPlanCell.class) owner:nil options:nil].lastObject;
    }
    
    if (indexPath.row == 0) {
        
        cell.topLineView.hidden = YES;
    }
    else if (indexPath.row == self.dataSource.count - 1) {
        
        cell.bottomLineView.hidden = YES;
    }
    
    FPRepayPlanModel *model = self.dataSource[indexPath.row];
    
    if (indexPath.row == 0) {
    
        cell.leftLab1.text = @"首期";
    }
    else {
    
        cell.leftLab1.text = [NSString stringWithFormat:@"%@期",model.installmentNumber];
    }
    
    cell.leftLab2.text = [FPLoanDetailModel timeOfParam:model.repaymentDate];
    
    cell.rightLab1.text = [NSString stringWithFormat:@"￥%.2f元", model.amount.floatValue];
    cell.rightLab2.text = [NSString stringWithFormat:@"本金%.2f元+利息%.2f元",model.principal.floatValue , model.interest.floatValue];
    
    return cell;
}


@end
